package com.summer_administration.mapper;

import com.summer_administration.pojo.Member;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface MemberMapper {
    // 新增方法：通过成员名称判断是否存在
    @Select("SELECT COUNT(*) FROM members WHERE member_name = #{name}")
    boolean existsByName(String name);

    @Insert("INSERT INTO members(member_name, gender, number, if_insure, create_user, create_time, update_time) " +
            "VALUES(#{memberName}, #{gender}, #{number}, #{ifInsure}, #{createUser}, #{createTime}, #{updateTime})")
    void add(Member member);

    @Select("SELECT * FROM members WHERE create_user = #{userId}")
    List<Member> listByUser(Integer userId);

    @Update("UPDATE members SET " +
            "member_name = #{memberName}, " +
            "gender = #{gender}, " +
            "number = #{number}, " +
            "if_insure = #{ifInsure}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    void update(Member member);

    @Delete("DELETE FROM members WHERE id = #{id}")
    void delete(Integer id);

    @Select("SELECT * FROM members WHERE id = #{id}")
    Member getById(Integer id);

    @Select("SELECT COUNT(*) FROM members WHERE number = #{number}")
    boolean existsByNumber(String number);

    @Select("SELECT COUNT(*) FROM members WHERE id = #{id}")
    int countGroupRelations(Integer memberId);

    // 新增查询方法
    @Select("SELECT id, member_name as memberName, gender, number, if_insure as ifInsure " +
            "FROM members WHERE member_name = #{memberName}")
    List<Member> selectByMemberName(String memberName);
}